package com.xbongbong.sys.model;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.ErrorMessageConstant;
import com.xbongbong.saas.domain.dao.SharePermissionSetUserDao;
import com.xbongbong.saas.domain.entity.SharePermissionSetUserEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author leex
 */
@Service("sharePermissionSetUserModel")
@Slf4j
public class SharePermissionSetUserModelImpl implements SharePermissionSetUserModel {

    @Resource
    private SharePermissionSetUserDao sharePermissionSetUserDao;

    @Override
    public Integer insert(SharePermissionSetUserEntity entity) {
        return sharePermissionSetUserDao.insert(entity);
    }

    @Override
    public void insertBatch(List<SharePermissionSetUserEntity> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        sharePermissionSetUserDao.insertBatch(list);
    }

    @Override
    public Integer update(SharePermissionSetUserEntity entity) throws XbbException {
        if (entity.getCorpid() == null) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, "corpid参数缺失！");
        }
        Integer num;
        try {
            num = sharePermissionSetUserDao.update(entity);
        } catch (Exception e) {
            log.error("共享规则更新出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, I18nMessageUtil.getMessage(ErrorMessageConstant.SAVE_ERROR));
        }
        return num;
    }

    @Override
    public void updateBatch(String corpid, List<SharePermissionSetUserEntity> list) throws XbbException {
        if (list.isEmpty()) {
            return;
        }
        sharePermissionSetUserDao.updateBatch(corpid, list);
    }


    @Override
    public Integer deleteByKey(Long key, String corpid) {
        return sharePermissionSetUserDao.deleteByKey(key, corpid);
    }


    @Override
    public Integer deleteByShareRuleId(Long shareRuleId, String corpid, Long formId) {
        return sharePermissionSetUserDao.deleteByShareRuleId(shareRuleId, corpid, formId);
    }

    @Override
    public Integer deleteByFormId(String corpid, Long formId) {
        return sharePermissionSetUserDao.deleteByFormId(corpid, formId);
    }

    @Override
    public void deleteBatch(String corpid, List<Long> idIn) {
        if (idIn.isEmpty()) {
            return;
        }
        sharePermissionSetUserDao.deleteBatch(corpid, idIn);
    }

    @Override
    public SharePermissionSetUserEntity getByKey(Long key, String corpid) {
        return sharePermissionSetUserDao.getByKey(key, corpid);
    }

    @Override
    public List<SharePermissionSetUserEntity> getByShareRuleId(Long shareRuleId, String corpid) {
        Map<String, Object> param = new HashMap<>();
        param.put("corpid", corpid);
        param.put("shareRuleId", shareRuleId);
        param.put("del", DelEnum.NORMAL.getDel());
        return sharePermissionSetUserDao.findEntitys(param);
    }

    @Override
    public List<SharePermissionSetUserEntity> findEntitys(Map<String, Object> param) {
        return sharePermissionSetUserDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return sharePermissionSetUserDao.getEntitysCount(param);
    }

    @Override
    public List<SharePermissionSetUserEntity> getEntitysByUsersAndForm(String corpid,Long formId, List<String> userIdIn) {
        Map<String,Object> params = new HashMap<>();
        params.put("corpid",corpid);
        params.put("formId",formId);
        params.put("userIdIn",userIdIn);
        params.put("del",0);
        return findEntitys(params);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return findEntitys(param);
    }
}
